

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="python" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="python" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>QUANTAXIS.QAAnalysis.QAAnalysis_trade &mdash; QUANTAXIS 1.0.39 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> QUANTAXIS
          

          
          </a>

          
            
            
              <div class="version">
                1.0.39
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">QUANTAXIS</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>QUANTAXIS.QAAnalysis.QAAnalysis_trade</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for QUANTAXIS.QAAnalysis.QAAnalysis_trade</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding=utf-8</span>
<span class="c1">#</span>
<span class="c1"># The MIT License (MIT)</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2016-2018 yutiansut/QUANTAXIS</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in all</span>
<span class="c1"># copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="c1"># SOFTWARE.</span>


<span class="kn">from</span> <span class="nn">statistics</span> <span class="k">import</span> <span class="n">mean</span>

<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>

<span class="kn">from</span> <span class="nn">QUANTAXIS.QAARP.QAAccount</span> <span class="k">import</span> <span class="n">QA_Account</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAARP.QARisk</span> <span class="k">import</span> <span class="n">QA_Performance</span><span class="p">,</span> <span class="n">QA_Risk</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAEngine.QAEvent</span> <span class="k">import</span> <span class="n">QA_Event</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAFetch.QAQuery</span> <span class="k">import</span> <span class="p">(</span><span class="n">QA_fetch_backtest_history</span><span class="p">,</span>
                                       <span class="n">QA_fetch_backtest_info</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAFetch.QAQuery_Advance</span> <span class="k">import</span> <span class="n">QA_fetch_stock_day_adv</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAMarket.QABacktestBroker</span> <span class="k">import</span> <span class="n">QA_BacktestBroker</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAUtil.QADate_trade</span> <span class="k">import</span> <span class="p">(</span><span class="n">QA_util_date_gap</span><span class="p">,</span>
                                           <span class="n">QA_util_get_next_day</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAUtil.QAParameter</span> <span class="k">import</span> <span class="p">(</span><span class="n">AMOUNT_MODEL</span><span class="p">,</span> <span class="n">ORDER_DIRECTION</span><span class="p">,</span>
                                          <span class="n">ORDER_MODEL</span><span class="p">)</span>


<div class="viewcode-block" id="QAAnalysis_trade"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAAnalysis.html#QUANTAXIS.QAAnalysis.QAAnalysis_trade.QAAnalysis_trade">[docs]</a><span class="k">class</span> <span class="nc">QAAnalysis_trade</span><span class="p">():</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Account/Portfolio是一个标准单元,所有成交记录分析 都会被加载到该单元中进行分析</span>
<span class="sd">    当我们只有一个成交记录的时候,我们会创建一个账户单元</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">init_cash</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">account</span> <span class="o">=</span> <span class="n">QA_Account</span><span class="p">(</span><span class="n">init_cash</span><span class="o">=</span><span class="n">init_cash</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">backtest_broker</span> <span class="o">=</span> <span class="n">QA_BacktestBroker</span><span class="p">()</span>

<div class="viewcode-block" id="QAAnalysis_trade.import_trade"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAAnalysis.html#QUANTAXIS.QAAnalysis.QAAnalysis_trade.QAAnalysis_trade.import_trade">[docs]</a>    <span class="k">def</span> <span class="nf">import_trade</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trade</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        trade是一个可迭代的list/generator</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">trade</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">make_deal</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">amount</span><span class="p">,</span>
                           <span class="n">item</span><span class="o">.</span><span class="n">towards</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">price</span><span class="o">.</span><span class="n">item</span><span class="o">.</span><span class="n">order_model</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">amount_model</span><span class="p">)</span></div>

<div class="viewcode-block" id="QAAnalysis_trade.make_deal"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAAnalysis.html#QUANTAXIS.QAAnalysis.QAAnalysis_trade.QAAnalysis_trade.make_deal">[docs]</a>    <span class="k">def</span> <span class="nf">make_deal</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">amount</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">towards</span><span class="o">=</span><span class="n">ORDER_DIRECTION</span><span class="o">.</span><span class="n">BUY</span><span class="p">,</span> <span class="n">price</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">order_model</span><span class="o">=</span><span class="n">ORDER_MODEL</span><span class="o">.</span><span class="n">MARKET</span><span class="p">,</span> <span class="n">amount_model</span><span class="o">=</span><span class="n">AMOUNT_MODEL</span><span class="o">.</span><span class="n">BY_AMOUNT</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        这是一个一定会成交,并且立刻结转(及t+0)的交易入口</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">receive_deal</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backtest_broker</span><span class="o">.</span><span class="n">receive_order</span><span class="p">(</span><span class="n">QA_Event</span><span class="p">(</span><span class="n">order</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">send_order</span><span class="p">(</span>
            <span class="n">code</span><span class="o">=</span><span class="n">code</span><span class="p">,</span> <span class="n">time</span><span class="o">=</span><span class="n">datetime</span><span class="p">,</span> <span class="n">amount</span><span class="o">=</span><span class="n">amount</span><span class="p">,</span> <span class="n">towards</span><span class="o">=</span><span class="n">towards</span><span class="p">,</span> <span class="n">price</span><span class="o">=</span><span class="n">price</span><span class="p">,</span> <span class="n">order_model</span><span class="o">=</span><span class="n">order_model</span><span class="p">,</span> <span class="n">amount_model</span><span class="o">=</span><span class="n">amount_model</span>
        <span class="p">))))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">settle</span><span class="p">()</span></div></div>



    <span class="c1"># @property</span>
    <span class="c1"># def codes(self):</span>
    <span class="c1">#     return self.code</span>

    <span class="c1"># def get_stock_tradehistory(self, code):</span>
    <span class="c1">#     return self.history.query(&#39;code==&quot;{}&quot;&#39;.format(code))</span>

    <span class="c1"># def get_stock_tradedetail(self, code):</span>
    <span class="c1">#     return self.detail.query(&#39;code==&quot;{}&quot;&#39;.format(code))</span>

    <span class="c1"># def get_loss_trade(self, num=5):</span>
    <span class="c1">#     return self.detail[self.detail.pnl_precentage &lt;= 0].sort_values(by=[&#39;pnl_precentage&#39;], ascending=True).head(num)</span>

    <span class="c1"># def get_profit_trade(self, num=5):</span>
    <span class="c1">#     return self.detail[self.detail.pnl_precentage &gt;= 0].sort_values(by=[&#39;pnl_precentage&#39;], ascending=False).head(num)</span>

    <span class="c1"># def get_trade_marketdata(self, rx, gap=3):</span>
    <span class="c1">#     return QA_fetch_stock_day_adv(rx.code.values[0], QA_util_date_gap(rx.date.values[0], gap, methods=&#39;lt&#39;), QA_util_date_gap(rx.sell_date.values[0][-1], gap, methods=&#39;gt&#39;))</span>

    <span class="c1"># def get_trade_before_and_after_pnl(self, rx, N=3, M=10):</span>
    <span class="c1">#     data = self.get_trade_marketdata(rx, M)</span>


<span class="k">def</span> <span class="nf">_mean</span><span class="p">(</span><span class="n">list_</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">list_</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">mean</span><span class="p">(</span><span class="n">list_</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;No Data&#39;</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, yutiansut.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'1.0.39',
            LANGUAGE:'python',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>